import require$$0$1 from "electron";
import require$$1 from "path";
import require$$0 from "fs";
var main = {};
const fs$1 = require$$0.promises;
const path$1 = require$$1;
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function processFiles$1(event, { inputPath, outputPath, replaceName }) {
  try {
    const files = await getAllFiles(inputPath);
    let current = 0;
    const total = files.length;
    for (const file of files) {
      const relativePath = path$1.relative(inputPath, file);
      event.sender.send("process-progress", {
        current,
        total,
        currentFile: relativePath
      });
      const newPath = path$1.join(outputPath, path$1.basename(inputPath), relativePath).replace(/copyme/gi, replaceName);
      await fs$1.mkdir(path$1.dirname(newPath), { recursive: true });
      try {
        const content = await fs$1.readFile(file, "utf-8");
        const newContent = content.replace(/copyme/gi, (match) => {
          if (match === match.toLowerCase()) {
            return replaceName.toLowerCase();
          } else if (match === "Copyme") {
            return replaceName.charAt(0).toUpperCase() + replaceName.slice(1);
          } else if (match === match.toUpperCase()) {
            return replaceName.toUpperCase();
          }
          return replaceName;
        });
        await fs$1.writeFile(newPath, newContent, "utf-8");
        await delay(500);
      } catch (readError) {
        console.warn(`无法处理文件内容: ${file}`, readError);
        await fs$1.copyFile(file, newPath);
        await delay(200);
      }
      current++;
      event.sender.send("process-progress", {
        current,
        total,
        currentFile: relativePath
      });
    }
    await delay(300);
    event.sender.send("process-progress", {
      current: total,
      total,
      currentFile: "处理完成"
    });
    return { success: true };
  } catch (error) {
    console.error("处理文件时出错:", error);
    return { success: false, error: error.message };
  }
}
async function getAllFiles(dir) {
  const files = [];
  const entries = await fs$1.readdir(dir, { withFileTypes: true });
  for (const entry of entries) {
    const fullPath = path$1.join(dir, entry.name);
    if (entry.isDirectory()) {
      files.push(...await getAllFiles(fullPath));
    } else {
      files.push(fullPath);
    }
  }
  return files;
}
var fileProcessor = { processFiles: processFiles$1 };
const { app, BrowserWindow, ipcMain, dialog, session } = require$$0$1;
const path = require$$1;
const fs = require$$0.promises;
const { processFiles } = fileProcessor;
let mainWindow = null;
function createWindow() {
  if (mainWindow) {
    return mainWindow;
  }
  mainWindow = new BrowserWindow({
    width: 1200,
    height: 800,
    webPreferences: {
      nodeIntegration: false,
      contextIsolation: true,
      sandbox: false,
      webSecurity: false,
      //是否禁用同源策略(上线时删除此配置)
      preload: path.join(__dirname, "preload.cjs")
    }
  });
  if (process.env.NODE_ENV === "development") {
    mainWindow.webContents.openDevTools();
  }
  const startUrl = process.env.VITE_DEV_SERVER_URL || "http://localhost:3004";
  mainWindow.loadURL(startUrl).catch(() => {
    console.log("Failed to load URL:", startUrl);
    const indexPath = path.join(__dirname, "../dist/index.html");
    if (fs.existsSync(indexPath)) {
      mainWindow.loadFile(indexPath);
    }
  });
  mainWindow.on("closed", () => {
    mainWindow = null;
  });
  return mainWindow;
}
app.whenReady().then(() => {
  createWindow();
  ipcMain.handle("select-folder", async () => {
    try {
      const result = await dialog.showOpenDialog(mainWindow, {
        properties: ["openDirectory"]
      });
      console.log("Dialog result:", result);
      return result;
    } catch (error) {
      console.error("Failed to show open dialog:", error);
      throw error;
    }
  });
  ipcMain.handle("check-directory", async (event, dirPath) => {
    try {
      const files = await fs.readdir(dirPath);
      return files.length > 0;
    } catch (error) {
      if (error.code === "ENOENT") {
        return false;
      }
      throw error;
    }
  });
  ipcMain.handle("show-confirm-dialog", async () => {
    const result = await dialog.showMessageBox(mainWindow, {
      type: "warning",
      title: "确认覆盖",
      message: "输出目录不为空，继续操作将覆盖现有文件。",
      detail: "是否继续？",
      buttons: ["继续", "取消"],
      defaultId: 1,
      cancelId: 1
    });
    return result.response === 0;
  });
  ipcMain.handle("process-files", processFiles);
  app.on("activate", () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow();
    }
  });
});
app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});
export {
  main as default
};
